import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list:[],
    lists:[],
    cart:[],
    show:false,
  },
  mutations: {
    addList(state,val){
      state.list = val
    },
    addLists(state,val){
      state.lists = val
      // console.log(val);
    },
    addShow(state,val){
      state.show = val
    },
    addCart(state,val){
      let show = true;
      state.cart.map(item=>{
        if(item.title==val.title){
          item.num = val.num
          show = false
          return
        }
      });
      if(show){
        state.cart.push(val)
      }
    },
    adddel(state){
      state.cart = [];
      state.lists.forEach(i=>{
        i.num = 0
      })
    },
    change(state,val){
      state.cart.forEach(i=>{
        if(i.title==val.title){
          i.num = val.num
        }
      })
    }
    
  },
  getters:{
    zj(state){
      let a = 0;
      state.cart.map(item=>{
        a += item.comm_price * item.num
      })
      return a * 100
    },
    num(state){
      let a = 0;
      state.cart.map(i=>{
        a += i.num
      })
      return a
    }
  },
  actions: {
  },
  modules: {
  }
})
